$(function() {

	// 加载设置
	var defaultConfig = {color: 'white'}; // 默认配置
	chrome.storage.sync.get(defaultConfig, function(items) {
		document.body.style.backgroundColor = items.color;
	});

	// 初始化国际化
	$('#test_i18n').html(chrome.i18n.getMessage("helloWorld"));
});

// faster Github Proxy 
$('#open_github_by_fast').click(e => {
	chrome.tabs.getSelected(null, function (tab) {
		const url = tab.url;
		let resUrl;
		if (url.indexOf('https://github.com') !== -1) {
			resUrl = `https://hub.fastgit.org/${url.split('https://github.com/')[1]}` 
			getCurrentTabId(tabId => {
				chrome.tabs.update(tabId, {url: resUrl});
			});
		} else {
			// alert("仅支持Github链接！")
		}
    });
})

// open nav
$('#open_nav').click(e => {
	getCurrentTabId(tabId => {
		chrome.tabs.update(tabId, {url: 'https://helson-lin.cn'});
	})
});	



// open background
$('#open_background').click(e => {
	window.open(chrome.extension.getURL('background.html'));
});


// 演示2种方式操作DOM

// 修改背景色
$('#update_bg_color').click(() => {
	executeScriptToCurrentTab('document.body.style.backgroundColor="#fffae8";')
});

// 修改字体大小
$('#update_font_size').click(() => {
	sendMessageToContentScript({cmd:'update_font_size', size: 42}, function(response){});
});

// 显示badge
$('#show_badge').click(() => {
	chrome.browserAction.setBadgeText({text: 'New'});
	chrome.browserAction.setBadgeBackgroundColor({color: [255, 0, 0, 255]});
});

// 隐藏badge
$('#hide_badge').click(() => {
	chrome.browserAction.setBadgeText({text: ''});
	chrome.browserAction.setBadgeBackgroundColor({color: [0, 0, 0, 0]});
});